package com.runssnail.demo.spring.web.action;

import com.eyeieye.melos.util.StringUtil;
import com.runssnail.demo.spring.biz.domain.MemberLoginDO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by zhengwei on 2016/2/4.
 */
@Controller
@RequestMapping("member")
public class LoginAction {

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public void login(@ModelAttribute("member") MemberLoginDO member) {

    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String doLogin(@ModelAttribute("member") MemberLoginDO member, HttpServletRequest request, Model model) {
        if (StringUtil.isBlank(member.getAccount()) || StringUtil.isBlank(member.getPassword())) {
            model.addAttribute("loginError", "账号或密码不能为空");
            return "member/login";
        }

        HttpSession session = request.getSession();
        session.setAttribute("member", member);

        return "redirect:/member/index.htm";
    }
}
